from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from selenium.webdriver.support.select import Select
import pyautogui
from selenium.webdriver.common.by import By
import xlrd

class FBA:

    def __init__(self):
        self.chrome_options = Options()
        self.driver = webdriver.Chrome(options=self.chrome_options)

    def excels(self):
        newtime=time.strftime('%y_%H_%d_%H_%m')
        debugs=open('./{}.csv'.format(newtime),'w',encoding='utf-8')
        self.liu01()
        self.liu02()
        #D:\测试01.xlsx
        books=xlrd.open_workbook(r'D:\测试01.xlsx')
        sheet = books.sheet_by_index(0)
        nrows=sheet.nrows
        print(nrows,type(nrows))
        for i in range(1,nrows):
            count=sheet.row_values(rowx=i)
            print(count)
            date_0,date_1,date_2,date_3,date_4,date_5,date_6,date_7,date_8,date_9,date_10,date_11,date_12,date_13=count[0],count[1],count[2],count[3],count[4],count[5],count[6],count[7],count[8],count[9],count[10],count[11],count[12],count[13]
            try:
                self.liu03(date_0,date_1,date_2,date_3,date_4,date_5,date_6,date_7,date_8,date_9,date_10,date_11,date_12,date_13)
                self.driver.refresh()
            except Exception as g:
                debugs.write('第{}个sku出现问题\n'.format(i))
                self.driver.refresh()
                continue
            # self.driver.refresh()

    def excles1(self):
        exceler='shipping_planq.xls'#input('请输入批量导入的excel的名称含后缀名')
        piliang=(r'C:\Users\Administrator\{}'.format(exceler))
        self.driver.find_element_by_css_selector('#FileName').send_keys(piliang)
        time.sleep(3)
        self.driver.find_element_by_css_selector('#batchAddForm > div.search-module-condition_batchAdd > input[type=button]').click()  # 点击提交

        self.driver.find_element_by_css_selector('#submitForm > div:nth-child(2) > div:nth-child(1) > div > input').click()#点击提交AMAZON

    def liu01(self):
        self.driver.get('http://demo.eccang.com/#')
        self.driver.maximize_window()
        self.driver.find_element_by_xpath('//*[@id="userName"]').send_keys('admin')
        self.driver.find_element_by_xpath('//*[@id="userPass"]').send_keys('eccang123456')
        self.driver.find_element_by_xpath('//*[@id="login"]').click()
        # self.driver.get('http://demo.eccang.com/#')

    def liu02(self):
        self.driver.implicitly_wait(30)
        self.driver.find_element_by_xpath('//*[@id="column"]/div[2]/ul[5]/li/a').click()
        # print(self.driver.page_source)
        self.driver.implicitly_wait(10)
        self.driver.find_element_by_xpath('//*[@id="headmenu17"]/ul[1]/ul/li[1]/a').click()
        # print(self.driver.page_source)

    def liu03(self,date_0,date_1,date_2,date_3,date_4,date_5,date_6,date_7,date_8,date_9,date_10,date_11,date_12,date_13):
        self.driver.implicitly_wait(10)
        self.driver.switch_to.frame("iframe-container-374")
        self.driver.implicitly_wait(10)
        self.driver.find_element_by_xpath('//*[@id="exportBtn"]').click()
        self.driver.switch_to.frame("dialogIframe")
        date01 = date_0  # 发货方式参数,供应商代发到目的仓
        if date01 == '供应商代发到目的仓':
            self.driver.find_element_by_css_selector(
                '#submitForm > div.fix_div > div:nth-child(3) > table > tbody > tr:nth-child(1) > td:nth-child(2) > label').click()
        else:
            self.driver.find_element_by_css_selector(
                '#submitForm > div.fix_div > div:nth-child(4) > table > tbody > tr:nth-child(1) > td:nth-child(2) > label').click()
        self.driver.find_element_by_css_selector('#warehouse_id_chosen > a > span').click()
        time.sleep(2)
        date02 = date_1  # 中转仓库,STORE1 [门店1]
        date02_eccanghuoiqu01 = self.driver.find_elements_by_css_selector(
            '#warehouse_id_chosen > div > ul > .active-result')
        # print(date02_eccanghuoiqu01)
        for i in date02_eccanghuoiqu01:
            if date02 == i.text:
                print(i.text)
                i.click()
        self.driver.find_element_by_css_selector('#to_warehouse_id_chosen > a > span').click()
        time.sleep(2)
        date03 = date_2  # 发往仓库,RM-CA-FBA [RM加拿大亚马逊FBA仓]
        date03_eccanghuoiqu02 = self.driver.find_elements_by_css_selector(
            '#to_warehouse_id_chosen > div > ul > .active-result')
        # print(date03_eccanghuoiqu02)
        for j in date03_eccanghuoiqu02:
            if date03 == j.text:
                print(j.text)
                j.click()

        date04 = date_3  # 头程运输方式,123WRWE[ubi德国专线UBI2971CN2EUR2971D]
        jim01 = self.driver.find_element_by_css_selector('#sm_code')
        Select(jim01).select_by_visible_text(date04)
        self.driver.find_element_by_css_selector('#plan_shipping_fee').send_keys('1')  # 预计头程费用

        self.driver.find_element_by_css_selector('#user_account_chosen > a > span').click()
        date05 = date_4  # 店铺账号,AC_Amazon_FR [AC_Amazon_FR]
        date05_eccanghuoiqu03 = self.driver.find_elements_by_css_selector(
            '#user_account_chosen > div > ul > .active-result')
        for k in date05_eccanghuoiqu03:
            if k.text == date05:
                print(k.text)
                k.click()

        date06 = date_5  # 贴标方,亚马逊
        jim02 = self.driver.find_element_by_css_selector('#label_type')
        Select(jim02).select_by_visible_text(date06)
        date07 = date_6  # 起始地,杭州桑美
        jim03 = self.driver.find_element_by_css_selector('#address_book')
        Select(jim03).select_by_visible_text(date07)
        self.driver.find_element_by_css_selector('#shipment_name').send_keys('1')#货件名称
        date08 = date_7  # 运输方式,汽运零担
        jim04 = self.driver.find_element_by_css_selector('#fba_shipping_method')
        Select(jim04).select_by_visible_text(date08)
        date09 = date_8  # 目标市场,[PL] 波兰
        jim05 = self.driver.find_element_by_css_selector('#country_code')
        Select(jim05).select_by_visible_text(date09)
        date10 = date_9#原包装发货
        if date10 == '原包装发货':
            self.driver.find_element_by_css_selector(
                '#submitForm > div:nth-child(2) > div:nth-child(3) > div:nth-child(2) > label:nth-child(3)').click()
        else:
            self.driver.find_element_by_css_selector(
                '#submitForm > div:nth-child(2) > div:nth-child(3) > div:nth-child(2) > label:nth-child(2)').click()
        date11=date_10#纸张类型,每张美国信纸上打印 1 个标签
        jim06=self.driver.find_element_by_css_selector('#page_type')
        Select(jim06).select_by_visible_text(date11)
        date12=date_11
        self.driver.find_element_by_css_selector('#ftpo_remark').send_keys(date12)
        date13=str(date_12)
        self.driver.find_element_by_css_selector('#ftpo_weight').send_keys(date13)  # 预计总重量
        date14=str(date_13)
        self.driver.find_element_by_css_selector('#ftpo_volume').send_keys(date14)  # 预计总体积
        self.driver.find_element_by_css_selector('#submitForm > div.search-module-condition > input').click()#保存并继续

        self.driver.find_element_by_css_selector('#batchAddBt').click()
        self.excles1()



if __name__ == '__main__':
    fba = FBA()
    # fba.liu01()
    # fba.liu02()
    # fba.liu03()
    fba.excels()